﻿@using Entidades.Sistema.Venta
@{
    
    Html.EnableClientValidation();
    Html.EnableUnobtrusiveJavaScript();

    var grid = Html.DevExpress().GridView(
        settings =>
        {
            settings.Name = "gvEditing";
            settings.KeyFieldName = "IdPedido";
            settings.CallbackRouteValues = new { Controller = "Pedidos", Action = "EditPedidosPartial", idcliente = ViewBag.IdCliente };
            settings.SettingsEditing.Mode = GridViewEditingMode.EditFormAndDisplayRow;
            
            settings.SettingsBehavior.ConfirmDelete = true;
            settings.SettingsPopup.EditForm.Width = 600;
            settings.SettingsPopup.EditForm.HorizontalAlign = PopupHorizontalAlign.Center;
            settings.SettingsPopup.EditForm.VerticalAlign = PopupVerticalAlign.WindowCenter;
            settings.SettingsPager.FirstPageButton.Visible = true;
            settings.SettingsPager.LastPageButton.Visible = true;
            settings.SettingsPager.PageSizeItemSettings.Visible = true;
            settings.SettingsPager.PageSizeItemSettings.Items = new string[] { "10", "20", "50" };
            
            settings.Width = System.Web.UI.WebControls.Unit.Percentage(100);

            settings.CommandColumn.Visible = false;
            settings.Columns.Add("IdPedido");
            settings.Columns.Add(
                column =>
                {
                    column.FieldName = "Cliente.IdCliente";
                    column.Caption = "Nombre Cliente";

                    column.ColumnType = MVCxGridViewColumnType.ComboBox;
                    var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
                    comboBoxProperties.DataSource = ViewBag.Cliente;
                    comboBoxProperties.TextField = "Nombre";
                    comboBoxProperties.ValueField = "IdCliente";
                    comboBoxProperties.ValueType = typeof(int);
                    comboBoxProperties.DropDownStyle = DropDownStyle.DropDown;
                    comboBoxProperties.ValidationSettings.CausesValidation = false;
                });

            settings.Columns.Add(column =>
            {
                column.FieldName = "Fecha";
                column.Caption = "Fecha Ingreso";
                column.ColumnType = MVCxGridViewColumnType.DateEdit;
                column.PropertiesEdit.DisplayFormatString = "dd/MM/yyyy";
            });

            settings.Columns.Add(column =>
            {
                column.FieldName = "FechaEntrega";
                column.Caption = "FechaEntrega";
                column.ColumnType = MVCxGridViewColumnType.DateEdit;
                column.PropertiesEdit.DisplayFormatString = "dd/MM/yyyy";
            });

            settings.Columns.Add(column =>
            {
                column.FieldName = "Total";

                column.ColumnType = MVCxGridViewColumnType.SpinEdit;
                column.Width = 100;
                var spinEditProperties = column.PropertiesEdit as SpinEditProperties;
                spinEditProperties.DisplayFormatString = "c";
                spinEditProperties.DisplayFormatInEditMode = true;
                spinEditProperties.MinValue = 0;
                spinEditProperties.MaxValue = 1000000;
                column.EditFormSettings.Visible = DevExpress.Utils.DefaultBoolean.False;
            });

            settings.Columns.Add(
                column =>
                {
                    column.FieldName = "Estado";
                    column.Caption = "Estado";

                    column.ColumnType = MVCxGridViewColumnType.ComboBox;
                    var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
                    comboBoxProperties.DataSource = ViewBag.Estado;
                    comboBoxProperties.ValueType = typeof(string);
                    comboBoxProperties.DropDownStyle = DropDownStyle.DropDown;
                });           
            
            
            settings.PreRender = (s, e) =>
            {
                var sender = (MVCxGridView)s;
                sender.FocusedRowIndex = 1;                
               
            };
            
            settings.HtmlDataCellPrepared = (sender, e) =>
            {
                if (e.DataColumn.FieldName == "Estado")
                {
                    if (e.CellValue.ToString() == ("Cancelado"))
                        e.Cell.BackColor = System.Drawing.Color.Coral;
                    if (e.CellValue.ToString() == ("Diagramado"))
                        e.Cell.BackColor = System.Drawing.Color.DodgerBlue;
                    if (e.CellValue.ToString() == ("Creado"))
                        e.Cell.BackColor = System.Drawing.Color.AliceBlue;
                    if (e.CellValue.ToString() == ("Entregado"))
                        e.Cell.BackColor = System.Drawing.Color.Aquamarine;
                    if (e.CellValue.ToString() == ("NoEntregado"))
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(85, 147, 247);
                    if (e.CellValue.ToString() == ("ParcDiagramado"))
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(19, 107, 248);
                }
            };

            settings.CommandButtonInitialize = (s, e) =>
            {
                if (e.ButtonType == ColumnCommandButtonType.Edit)
                {
                    MVCxGridView g = s as MVCxGridView;
                    string value = g.GetRowValues(e.VisibleIndex, "Estado").ToString(); //use a correct field name and cast a resultant value to a correct value type

                    if (value == ("Cancelado"))
                        e.Visible = false;
                    if (value == ("Diagramado"))
                        e.Visible = false;
                    if (value == ("Entregado"))
                        e.Visible = false;
                    if (value == ("NoEntregado"))
                        e.Visible = false;
                    if (value == ("ParcDiagramado"))
                        e.Visible = false;
                    if (value == ("Producido"))
                        e.Visible = false;
                }
            };
            
            settings.SettingsBehavior.AllowFocusedRow = true;

            settings.BeforeGetCallbackResult = (sender, e) =>
            {
                MVCxGridView grid2 = (MVCxGridView)sender;
                if (ViewData["Pedido"] != null)
                {
                    grid2.MakeRowVisible(ViewData["Pedido"]);
                    grid2.DetailRows.ExpandRowByKey(ViewData["Pedido"]);
                }
            };         
        });

	if (ViewData["EditError"] != null)
	{
		grid.SetEditErrorText((string)ViewData["EditError"]);
	}
}
@grid.Bind(Model).GetHtml()




